package it.sisisoftware.jogmobile;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class  JogM extends ListActivity {
	private static final int MENU_ESTRATTO_CONTO = 0;
	private static final int MENU_CLIENTI = 1;
	private static final int MENU_FORNITORI = 2;
	private static final int MENU_CONFIGURAZIONE = 3;
	private static final int MENU_OTTIENILOGIN = 4;
	private static final int MENU_COMANDI_VOCALI = 5;
	private static final int MENU_HELP_COMANDI_VOCALI = 6;

	private EfficientAdapter adap;
	private static String[] data = new String[] { "0", "1", "2", "3", "4", "5", "6" };
	private static String[] opzioni_menu;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
	  super.onCreate(savedInstanceState);
	  opzioni_menu = getResources().getStringArray(R.array.array_opzioni_menu);
	  
		setContentView(R.layout.main2);
		adap = new EfficientAdapter(this);
		setListAdapter(adap);
	  
	  /*
	  setContentView(R.layout.list_generale);
	  opzioni_menu = getResources().getStringArray(R.array.array_opzioni_menu);
	  //setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, opzioni_menu));
	  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, opzioni_menu));
*/
		

	  ListView lv = getListView();
	  lv.setTextFilterEnabled(true);

	  lv.setOnItemClickListener(new OnItemClickListener() {
	    public void onItemClick(AdapterView<?> parent, View view,
	        int position, long id) {
	    	      
	       switch(position) {
           case MENU_ESTRATTO_CONTO:
        	   //new ViewEstrattoConto();
        	   vedoEC();
        	   break;
           case MENU_CLIENTI:
           case MENU_FORNITORI:
	       		SharedPreferences pref =
	                PreferenceManager.getDefaultSharedPreferences(
	                		JogM.this);
	    		String ut = pref.getString("utente", "jogdemo.admin");
	    		String pa = pref.getString("password", "admin");
	    		
	    		ut = (ut==""?"jogdemo.admin":ut);
	    		pa = (pa==""?"admin":pa);
	    		
	    		
	    		if (ut.equals("") || pa.equals("")){
	    			Toast.makeText(getApplicationContext(),"Inserire utente e password nella configurazione",
	    			          Toast.LENGTH_SHORT).show();
	    			return;
	    		}
	    		if (!Networking.isNetworkAvailable(JogM.this)){
	    			Toast.makeText(getApplicationContext(),"Controllare collegamento Internet",
	    			          Toast.LENGTH_SHORT).show();
	    			return;
	    		}
	    		
	         	vedoNominativi(position);
        	   break;
           case MENU_CONFIGURAZIONE:
        	   vedoConfigurazione();
        	   break;
        	   
           case MENU_OTTIENILOGIN:
        	   datiperRegistrazione();
        	   break;
        	   
           case MENU_COMANDI_VOCALI:
        	   comandiVocali();
        	   break;
        	   
           case MENU_HELP_COMANDI_VOCALI:
        	   helpcomandiVocali();
        	   break;
	       }

	      
	      
	    }
	  });
	  
	  
	}
	
	
	private void vedoEC(){
 	  //Intent intent = new Intent(JogM.this, ViewEstrattoConto.class);
 	  //startActivity(intent);
 	  Intent intent = new Intent(JogM.this, ViewECTotale.class);
 	  startActivity(intent);
	}
	
	private void vedoNominativi(int position){	
		
	 	  Intent intent = new Intent(JogM.this, ViewNominativi.class);
	 	  Bundle bundle = new Bundle();
	 	  bundle.putString("OPERAZIONE", (position==1?"CLIENTI":"FORNITORI") );
	 	  intent.putExtras(bundle);
	 	  startActivity(intent);
		}
	
	private void vedoConfigurazione(){
	 	  Intent intent = new Intent(JogM.this, Configurazione.class);
	 	  startActivity(intent);
		}
	
	private void datiperRegistrazione(){
	 	  Intent intent = new Intent(JogM.this, Registrazione.class);
	 	  startActivity(intent);
		}
	
	private void comandiVocali(){
	 	  Intent intent = new Intent(JogM.this, Comandivocali.class);
	 	  startActivity(intent);
		}
	
	private void helpcomandiVocali(){
	 	 // Intent intent = new Intent(JogM.this, Helpcomandivocali.class);
	 	 // startActivity(intent);
		
		 Intent intent = new Intent(JogM.this, Helpcomandivocali.class);
	 	  startActivity(intent);
		}
	
	
	public static class EfficientAdapter extends BaseAdapter implements Filterable {
	    private LayoutInflater mInflater;
	    private Bitmap mIcon1;
	    private Context context;

	    public EfficientAdapter(Context context) {
	      // Cache the LayoutInflate to avoid asking for a new one each time.
	      mInflater = LayoutInflater.from(context);
	      this.context = context;
	    }

	    /**
	     * Make a view to hold each row.
	     * 
	     * @see android.widget.ListAdapter#getView(int, android.view.View,
	     *      android.view.ViewGroup)
	     */
	    public View getView(final int position, View convertView, ViewGroup parent) {
	      // A ViewHolder keeps references to children views to avoid
	      // unneccessary calls
	      // to findViewById() on each row.
	      ViewHolder holder;

	      // When convertView is not null, we can reuse it directly, there is
	      // no need
	      // to reinflate it. We only inflate a new View when the convertView
	      // supplied
	      // by ListView is null.
	      if (convertView == null) {
	        convertView = mInflater.inflate(R.layout.adaptor_content, null);

	        // Creates a ViewHolder and store references to the two children
	        // views
	        // we want to bind data to.
	        holder = new ViewHolder();
	        holder.textLine = (TextView) convertView.findViewById(R.id.textLine);
	        holder.iconLine = (ImageView) convertView.findViewById(R.id.iconLine);
	        convertView.setTag(holder);
	      } else {
	        // Get the ViewHolder back to get fast access to the TextView
	        // and the ImageView.
	        holder = (ViewHolder) convertView.getTag();
	      }


	      String filename = "";
	      
	      
	      switch (position) {
		case 0:		
			filename = "estratto_conto";
			break;
		case 1:			
			filename = "clienti";
			break;
		case 2:			
			filename = "fornitori";
			break;
		case 3:			
			filename = "configurazione";
			break;
		case 4:			
			filename = "credenziali";
			break;
		case 5:			
			filename = "comandi_vocali";
			break;
		case 6:			
			filename = "help";
			break;

		default:
			break;
		}
	      
	      

	      int id = context.getResources().getIdentifier(filename, "drawable", "it.sisisoftware.jogmobile");

	      // Icons bound to the rows.
	      if (id != 0x0) {
	        mIcon1 = BitmapFactory.decodeResource(context.getResources(), id);
	      }

	      // Bind the data efficiently with the holder.
	      holder.iconLine.setImageBitmap(mIcon1);
	      holder.textLine.setText(opzioni_menu[position]);	      
	      //opzioni_menu[

	      return convertView;
	    }

	    static class ViewHolder {
	      TextView textLine;
	      ImageView iconLine;
	      Button buttonLine;
	    }

	    @Override
	    public Filter getFilter() {
	      // TODO Auto-generated method stub
	      return null;
	    }

	    @Override
	    public long getItemId(int position) {
	      // TODO Auto-generated method stub
	      return 0;
	    }

	    @Override
	    public int getCount() {
	      // TODO Auto-generated method stub
	      return data.length;
	    }

	    @Override
	    public Object getItem(int position) {
	      // TODO Auto-generated method stub
	      return data[position];
	    }

	  }
	
}